package org.javaforever.gatescore.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.core.FrontCodeBlock;
import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.utils.StringUtil;

public class ChangeMyPasswordUser extends FrontVerb  {
	public ChangeMyPasswordUser(FrontDomain domain) {
		super();
		this.domain = domain;
		this.verbName = "ChangeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName();
	}

	@Override
	public FrontMethod generateControllerMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName(StringUtil.capFirst(this.getVerbName()));
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"() {"));
		sList.add(new Statement(2000L,1,"var old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = this.form.old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()));
		sList.add(new Statement(3000L,1,"var "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+" = this.form."+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()));
		sList.add(new Statement(4000L,1,"var confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" = this.form.confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()));
		sList.add(new Statement(5000L,1,"if ("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+" !== confirm"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+") {"));
		if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
			sList.add(new Statement(6000L,2,"this.$alert('New "+this.domain.findFieldByFixedName("password").getText()+" not match.')"));
		} else {
			sList.add(new Statement(6000L,2,"this.$alert('新"+this.domain.findFieldByFixedName("password").getText()+"不匹配！')"));
		}
		sList.add(new Statement(6100L,2,"return"));
		sList.add(new Statement(6500L,1,"}"));
		sList.add(new Statement(7000L,1,"if (old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+" === "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+") {"));
		if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
			sList.add(new Statement(8000L,2,"this.$alert('Old and new "+this.domain.findFieldByFixedName("password").getText()+" can not be same.')"));
		} else {
			sList.add(new Statement(8000L,2,"this.$alert('新旧"+this.domain.findFieldByFixedName("password").getText()+"不可以相同！')"));
		}
		sList.add(new Statement(9000L,2,"return"));
		sList.add(new Statement(9500L,1,"}"));
		sList.add(new Statement(10000L,1,"changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+", "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+").then(response => {"));
		sList.add(new Statement(11000L,2,"if (response.data.success === true) {"));
		if ("english".equalsIgnoreCase(this.domain.getLanguage())) {
			sList.add(new Statement(12000L,3,"this.$message.success('Successfully changed "+this.domain.findFieldByFixedName("password").getText()+".')"));
			sList.add(new Statement(13000L,2,"} else if (response.data.success === false){"));
			sList.add(new Statement(14000L,3,"this.$message.success('Change "+this.domain.findFieldByFixedName("password").getText()+" failed.')"));
			sList.add(new Statement(15000L,2,"} else {"));
			sList.add(new Statement(16000L,3,"this.$message.error('Change "+this.domain.findFieldByFixedName("password").getText()+" failed.')"));
		}else {
			sList.add(new Statement(12000L,3,"this.$message.success('成功修改"+this.domain.findFieldByFixedName("password").getText()+"！')"));
			sList.add(new Statement(13000L,2,"} else if (response.data.success === false) {"));
			sList.add(new Statement(14000L,3,"this.$message.success('修改"+this.domain.findFieldByFixedName("password").getText()+"失败！')"));
			sList.add(new Statement(15000L,2,"} else {"));
			sList.add(new Statement(16000L,3,"this.$message.error('修改"+this.domain.findFieldByFixedName("password").getText()+"失败！')"));
		}
		sList.add(new Statement(17000L,2,"}"));
		sList.add(new Statement(18000L,2,"this.changePasswordDialogVisible = false"));
		sList.add(new Statement(18000L,2,"findMyProfile"+this.domain.getCapFirstDomainName()+"()"));
		sList.add(new Statement(19000L,1,"})"));
		sList.add(new Statement(20000L,0,"},"));
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontMethod generateApiMethod() throws Exception {
		FrontMethod method = new FrontMethod();
		method.setStandardName("changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getLowerFirstDomainName());
		method.addAdditionalImport("import sha1 from 'sha1'");
		List<Writeable> sList = new ArrayList<Writeable>();
		
		sList.add(new Statement(1000L,0,"export function changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+", "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+") {"));
		sList.add(new Statement(2000L,1,"return request({"));
		sList.add(new Statement(3000L,2,"url: 'profile"+this.domain.getControllerNamingSuffix()+"/changeMy"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+this.domain.getCapFirstDomainName()+"',"));
		sList.add(new Statement(4000L,2,"method: 'post',"));
		if (this.domain.isUseOnlyJson() == true) {
			sList.add(new Statement(5000L,2,"data: { old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+": sha1(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"), "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+": sha1("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+") }"));
		} else {
			sList.add(new Statement(5000L,2,"data: qs.stringify({ old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+": sha1(old"+this.domain.findFieldByFixedName("password").getCapFirstFieldName()+"), "+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+": sha1("+this.domain.findFieldByFixedName("password").getLowerFirstFieldName()+") })"));
		}
		sList.add(new Statement(6000L,1,"})"));
		sList.add(new Statement(7000L,0,"}"));
		
		method.setMethodStatementList(WriteableUtil.merge(sList));
		return method;
	}

	@Override
	public FrontCodeBlock generateRouteBlockBlock() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public FrontMethod generateControllerMethod(String indexSuffix) throws Exception {
		return generateControllerMethod();
	}
}
